#include "include/muduo/net/TcpClient.h"
#include "include/muduo/net/TcpConnection.h"
#include "include/muduo/net/EventLoopThread.h"
#include "include/muduo/base/CountDownLatch.h"

#include "include/muduo/proto/codec.h"
#include "include/muduo/proto/dispatcher.h"
#include "include/muduo/base/Logging.h"     //不加incllude可以吗
#include "include/muduo/base/Mutex.h"
#include "protocol.pb.h"

#include <memory>
#include <iostream>
#include <functional>
#include <string>
#include <unistd.h>
using namespace std::placeholders;
class Client
{
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<trans_add::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<trans_add::AddResponse> AddResponsePtr;
public:
    Client(const std::string& serverip, uint16_t serverport)
        :_latch(1)
        ,_client(_loopthread.startLoop(), muduo::net::InetAddress(serverip, serverport), "client")
        ,_dispatcher(std::bind(&Client::onUnknownMessage, this, _1, _2, _3))
        ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, _1, _2, _3))
    {
        //模板别忘了
        _dispatcher.registerMessageCallback<trans_add::TranslateResponse>(std::bind(&Client::OnTranslateCallback, this, _1, _2, _3));
        _dispatcher.registerMessageCallback<trans_add::AddResponse>(std::bind(&Client::OnAddCallback, this, _1, _2, _3));
        _client.setConnectionCallback(std::bind(&Client::OnConnectionCallback, this, _1));
        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, _1, _2, _3));
    }
    void Connect()
    {
        _client.connect();
        _latch.wait();
    }
    void TranslateRequest(const std::string& str)
    {
        trans_add::TranslateRequest request;
        request.set_request(str);
        _codec.send(_conn, request);
    }
    void AddRequest(int num1, int num2)
    {
        trans_add::AddRequest request;
        request.set_num1(num1);
        request.set_num2(num2);
        _codec.send(_conn, request);
    }
private:

    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn,
                        const MessagePtr& message,
                        muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
    void OnTranslateCallback(const muduo::net::TcpConnectionPtr& conn,
                        const TranslateResponsePtr& message,
                        muduo::Timestamp)
    {
        // LOG_INFO << message->response() << std::endl;  // ???
        std::cout << message->response() << std::endl;  
    }
    void OnAddCallback(const muduo::net::TcpConnectionPtr& conn,
                        const AddResponsePtr& message,
                        muduo::Timestamp)
    {
        std::cout << message->result() << std::endl;
    }
    void OnConnectionCallback(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected())
        {
            _latch.countDown();
            _conn = conn;
            std::cout << "链接成功" << std::endl;
        }
        else 
        {
            _conn.reset();            
            std::cout << "链接断开" << std::endl;
        }
    }
private:
    muduo::CountDownLatch _latch;
    muduo::net::EventLoopThread _loopthread;
    muduo::net::TcpClient _client;
    muduo::net::TcpConnectionPtr _conn;

    ProtobufDispatcher _dispatcher;
    ProtobufCodec _codec;
};

int main(int argc, char* argv[])
{
    std::unique_ptr<Client> client = std::make_unique<Client>(argv[1], static_cast<uint16_t>(std::stoi(argv[2])));
    client->Connect();
    client->TranslateRequest("apple");
    client->AddRequest(1, 3);
    sleep(1);
    return 0;
}